<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\BaseController;
use app\common\model\Product\Product as ProductModel;
use app\common\model\Product\Vip as VipModel;
use app\common\validate\Product\Vip as VipValidate;
use think\exception\ValidateException;


class Vip extends BaseController
{
    protected $middleware = [
        \app\admin\middleware\LoginAuth::class
    ];
    public function index()
    {
        $proid = $this->request->param('proid');
        $Product = ProductModel::find($proid);
        if (!$Product) {
            $this->error('商品不存在');
        }
        $menu = [
            'name' => '商品管理',
            'title' => "{$Product['name']}商品会员等级价格"
        ];
        $where[] = ['proid', '=', $proid];
        $num = 8;
        $VipList = VipModel::where($where)->paginate(['list_rows' => $num, 'query' => request()->param()]);
        // 当前页
        $page = $VipList->currentPage();
        return view('', [
            'menu' => $menu,
            'proid' => $proid,
            'VipList' => $VipList,
            'num' => $num,
            'page' => $page,
        ]);
    }
    public function add()
    {
        $proid = $this->request->param('proid');
        $Product = ProductModel::find($proid);
        if (!$Product) {
            $this->error('商品不存在');
        }
        $menu = [
            'name' => '商品管理',
            'title' => "新增{$Product['name']}商品会员等级价格"
        ];
        if ($this->request->isPost()) {
            $level = $this->request->param('level');
            $price = $this->request->param('price');
            $VipPrice = VipModel::where(['proid' => $proid,'level' => $level])->find();
            if ($VipPrice) {
                $this->error('此等级已有会员价格！');
            }
            $data = [
                'level' => $level,
                'price' => $price,
                'proid' => $proid
            ];
            try {
                Validate(VipValidate::class)->check($data);
            } catch (ValidateException $e) {
                $this->error($e->getMessage());
            }
            $result = VipModel::create($data);
            if ($result === false) {
                $this->error('新增失败');
            } else {
                $this->success('新增成功', (string)url('admin/vip/index', ['proid' => $proid]));
            }
        }
        return view('', [
            'menu' => $menu,
            'Product' => $Product
        ]);
    }
    // 异步查询该等级有无会员价格
    public function select()
    {
        if ($this->request->isAjax()) {
            $proid = $this->request->param('proid');
            $level = $this->request->param('level');
            $Product = ProductModel::find($proid);
            if (!$Product) {
                $this->error('商品不存在');
            }
            $VipPrice = VipModel::where(['proid' => $proid,'level' => $level])->find();
            if ($VipPrice) {
                $this->error('此等级已有会员价格！');
            }
        }
    }
    public function edit(){
        $proid = $this->request->param('proid');
        $Product = ProductModel::find($proid);
        if (!$Product) {
            $this->error('商品不存在');
        }
        $menu = [
            'name' => '商品管理',
            'title' => "编辑{$Product['name']}商品会员等级价格"
        ];
        $vipid = $this->request->param('vipid');
        $Vip = VipModel::find($vipid);
        if (!$Vip) {
            $this->error('会员价格不存在');
        }
        if ($this->request->isPost()) {
            $level = $this->request->param('level');
            $price = $this->request->param('price');
            $VipPrice = VipModel::where(['proid' => $proid,'level' => $level])->find();
            if ($VipPrice) {
                $this->error('此等级已有会员价格！');
            }
            $data = [
                'level' => $level,
                'price' => $price,
            ];
            try {
                Validate(VipValidate::class)->scene('edit')->check($data);
            } catch (ValidateException $e) {
                $this->error($e->getMessage());
            }
            $result = VipModel::update($data,['id'=>$vipid]);
            if ($result === false) {
                $this->error('修改失败');
            } else {
                $this->success('修改成功', (string)url('admin/vip/index', ['proid' => $proid]));
            }
        }
        return view('', [
            'menu' => $menu,
            'Product' => $Product,
            'Vip'=>$Vip
        ]);
    }
    public function del(){
        $vipid=$this->request->param('vipid');
        $ids=$this->request->param('ids');
        $action=$this->request->param('action');
        if($action == 'del'){
            $Vip=VipModel::find($vipid);
            if(!$Vip){
                $this->error('该会员价格不存在');
            }
            $result =VipModel::destroy($vipid);
            if($result===FALSE){
                $this->error('删除失败');
            }
            $this->success('删除成功');
        }
        if($action=='dels'){
            $VipList = VipModel::whereIn('id', $ids)->select();
            foreach ($VipList as $key => $item) {
                if (!in_array($item['id'], $ids)) {
                    $this->error("所选会员价格不存在,请重新选择");
                }
            }
            $result = VipModel::destroy(['id' => $ids]);
            if ($result === false) {
                $this->error('删除失败');
            }
            $this->success('删除成功');
        }
    }
}
